package com.dingwei.superiordiary.mvp.presenter;

import android.os.CountDownTimer;
import android.support.v4.widget.NestedScrollView;
import android.text.TextUtils;

import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;
import com.lihao.baselibrary.utils.RegexUtils;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Constructor;

/**
 * Created by lihao on 2017/9/12.
 */

public class ForgetPasswordPresenter extends basePresenter {

    private Contract.ForgetPasswordView forgetPasswordView;

    public ForgetPasswordPresenter(Contract.ForgetPasswordView view) {
        super(view);
        this.forgetPasswordView = view;
    }

    public void getCode(String mobile) {
        if (TextUtils.isEmpty(mobile)) {
            showToast("请输入手机号码");
            return;
        }
        if (!RegexUtils.isMobileExact(mobile)) {
            showToast("请输入正确的手机号码");
            return;
        }

        
        RequestParams rp = new RequestParams(URLS.HOST+URLS.GET_VERIFY_CODE);
        rp.addBodyParameter("mobile",mobile);
        rp.addBodyParameter("type","2");
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                MyCount myCount = new MyCount(60000,1000);
                myCount.start();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }

    public void forgetPassword(String mobile, String code, String password, String re_password) {
        if (TextUtils.isEmpty(mobile)) {
            showToast("请输入手机号码");
            return;
        }
        if (!RegexUtils.isMobileExact(mobile)) {
            showToast("请输入正确的手机号码");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            showToast("请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        if (TextUtils.isEmpty(re_password)) {
            showToast("请再次输入密码");
            return;
        }
        RequestParams rp =new RequestParams(URLS.HOST+URLS.FORGET_PASSWORD);
        rp.addBodyParameter("mobile",mobile);
        rp.addBodyParameter("code",code);
        rp.addBodyParameter("password",password);
        rp.addBodyParameter("re_password",re_password);

        x.http().post(rp, new myCallback(BaseActivity.context,MyBaseBean.class,rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                forgetPasswordView.forgetPasswordSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }


    /**
     * 倒计时
     */
    public class MyCount extends CountDownTimer {


        public MyCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            forgetPasswordView.CountDownTimerFinish();
        }

        @Override
        public void onTick(long millisUntilFinished) {
            forgetPasswordView.CountDownTimeronTick(millisUntilFinished);
        }

    }
}
